package com.xhwl.logistics.bo;

import com.xhwl.common.pojo.cent.vehicle.VehcCarRecord;
import com.xhwl.logistics.bo.input.QueryVehicleAccessRecordStatisticsBO;
import com.xhwl.logistics.po.VehicleAccessRecordStatisticsDatePO;
import com.xhwl.logistics.util.AboutDateUtil;
import lombok.Getter;
import lombok.Setter;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Setter
@Getter
public class VehicleAccessRecordStatisticsBO {

    private String parkingLotName;

    private Integer carportNumber;

    private Integer freeCarportNumber;

    private String homeAddress;

    private Integer enterAccessRecordNumber;

    private Integer enterLocalVehicleNumber;

    private Integer enterForeignVehicleNumber;

    private Integer existAccessRecordNumber;

    private Integer existLocalVehicleNumber;

    private Integer existForeignVehicleNumber;

    private List<VehicleAccessRecordStatisticsDateBO> enter;

    private List<VehicleAccessRecordStatisticsDateBO> exist;

    private void initAboutHour(Date startTime, Date endTime) {
        enter = new ArrayList<>();
        exist = new ArrayList<>();
        for (Date start=startTime; start.before(endTime); start=AboutDateUtil.datePlusHour(start, 1)) {
            VehicleAccessRecordStatisticsDateBO vehicleAccessRecordStatisticsDateBO = new VehicleAccessRecordStatisticsDateBO();
            vehicleAccessRecordStatisticsDateBO.setCount(0);
            vehicleAccessRecordStatisticsDateBO.setDate(AboutDateUtil.dateToString(start, "yyyy-MM-dd HH"));
            enter.add(vehicleAccessRecordStatisticsDateBO);
            exist.add(vehicleAccessRecordStatisticsDateBO.copyVehicleAccessRecordStatisticsDateBO());
        }
    }

    private void initAboutDay(Date startTime, Date endTime) {
        enter = new ArrayList<>();
        exist = new ArrayList<>();
        for (Date start=startTime; start.before(endTime); start=AboutDateUtil.datePlusDay(start, 1)) {
            VehicleAccessRecordStatisticsDateBO vehicleAccessRecordStatisticsDateBO = new VehicleAccessRecordStatisticsDateBO();
            vehicleAccessRecordStatisticsDateBO.setCount(0);
            vehicleAccessRecordStatisticsDateBO.setDate(AboutDateUtil.dateToString(start, "yyyy-MM-dd"));
            enter.add(vehicleAccessRecordStatisticsDateBO);
            exist.add(vehicleAccessRecordStatisticsDateBO.copyVehicleAccessRecordStatisticsDateBO());
        }
    }

    private void initAboutMonth(Date startTime, Date endTime) {
        enter = new ArrayList<>();
        exist = new ArrayList<>();
        for (Date start=startTime; start.before(endTime); start=AboutDateUtil.datePlusMonth(start, 1)) {
            VehicleAccessRecordStatisticsDateBO vehicleAccessRecordStatisticsDateBO = new VehicleAccessRecordStatisticsDateBO();
            vehicleAccessRecordStatisticsDateBO.setCount(0);
            vehicleAccessRecordStatisticsDateBO.setDate(AboutDateUtil.dateToString(start, "yyyy-MM"));
            enter.add(vehicleAccessRecordStatisticsDateBO);
            exist.add(vehicleAccessRecordStatisticsDateBO.copyVehicleAccessRecordStatisticsDateBO());
        }
    }

    public VehicleAccessRecordStatisticsBO(Integer type, Date startTime, Date endTime) {
        if (type.equals(0)) {
            initAboutHour(startTime, endTime);
        } else if (type.equals(1)) {
            initAboutDay(startTime, endTime);
        } else if (type.equals(2)) {
            initAboutDay(startTime, endTime);
        } else if (type.equals(3)) {
            initAboutMonth(startTime, endTime);
        } else if (type.equals(-1)) {
            initAboutDay(startTime, endTime);
        }
        //方式为今日
        else if(type.equals(4)){
            initAboutHour(startTime, endTime);
        }
        //方式为本周
        else if(type.equals(5)){
            initAboutDay(startTime, endTime);
        }
        //方式为本月
        else if(type.equals(6)){
            initAboutDay(startTime, endTime);
        }
        else {
            throw new IllegalArgumentException("车辆进出场记录统计的type值非法，无法完成显示数据的转换");
        }
    }

    public static VehicleAccessRecordStatisticsBO toVehicleStatisticsBO(String parkingLotName, Integer carportNumber, Integer freeCarportNumber, String homeAddress, QueryVehicleAccessRecordStatisticsBO queryVehicleAccessRecordStatisticsBO, List<VehcCarRecord> enterTodayVehicleAccessRecordList, List<VehcCarRecord> existTodayVehicleAccessRecordList, List<VehicleAccessRecordStatisticsDatePO> enterVehicleAccessRecordStatisticsDatePOList, List<VehicleAccessRecordStatisticsDatePO> existVehicleAccessRecordStatisticsDatePOList) {
        VehicleAccessRecordStatisticsBO vehicleAccessRecordStatisticsBO = new VehicleAccessRecordStatisticsBO(queryVehicleAccessRecordStatisticsBO.getType(), queryVehicleAccessRecordStatisticsBO.getStartTime(), queryVehicleAccessRecordStatisticsBO.getEndTime());
        vehicleAccessRecordStatisticsBO.setParkingLotName(parkingLotName);
        vehicleAccessRecordStatisticsBO.setCarportNumber(carportNumber);
        vehicleAccessRecordStatisticsBO.setFreeCarportNumber(freeCarportNumber);
        vehicleAccessRecordStatisticsBO.setHomeAddress(homeAddress);

        int enterAccessRecordNumber = enterTodayVehicleAccessRecordList.size();
        long enterLocalVehicleNumber = enterTodayVehicleAccessRecordList.stream().filter(vehcCarRecord -> {
            String plateNumber = vehcCarRecord.getPlateNumber();
            if (null != plateNumber) {
//                return plateNumber.contains(homeAddress);
                String[] plateNumbers = homeAddress.split(",");
                for (String plateNumberFromHome : plateNumbers) {
                    if(plateNumber.contains(plateNumberFromHome)){
                        return true;
                    }
                }
                return false;
            }
            return false;
        }).count();
        int enterForeignVehicleNumber = enterAccessRecordNumber - (int)enterLocalVehicleNumber;

        int existAccessRecordNumber = existTodayVehicleAccessRecordList.size();
        long existLocalVehicleNumber = existTodayVehicleAccessRecordList.stream().filter(vehcCarRecord -> {
            String plateNumber = vehcCarRecord.getPlateNumber();
            if (null != plateNumber) {
//                return plateNumber.contains(homeAddress);
                String[] plateNumbers = homeAddress.split(",");
                for (String plateNumberFromHome : plateNumbers) {
                    if(plateNumber.contains(plateNumberFromHome)){
                        return true;
                    }
                }
                return false;
            }
            return false;
        }).count();
        int existForeignVehicleNumber = (existAccessRecordNumber - (int)existLocalVehicleNumber);

        enterVehicleAccessRecordStatisticsDatePOList.forEach(vehicleAccessRecordStatisticsDatePO -> {
            String date = vehicleAccessRecordStatisticsDatePO.getDate();
            vehicleAccessRecordStatisticsBO.getEnter().forEach(vehicleAccessRecordStatisticsDateBO -> {
                if (date.equals(vehicleAccessRecordStatisticsDateBO.getDate())) {
                    vehicleAccessRecordStatisticsDateBO.setCount(vehicleAccessRecordStatisticsDatePO.getCount());
                }
            });
        });

        existVehicleAccessRecordStatisticsDatePOList.forEach(vehicleAccessRecordStatisticsDatePO -> {
            String date = vehicleAccessRecordStatisticsDatePO.getDate();
            vehicleAccessRecordStatisticsBO.getExist().forEach(vehicleAccessRecordStatisticsDateBO -> {
                if (date.equals(vehicleAccessRecordStatisticsDateBO.getDate())) {
                    vehicleAccessRecordStatisticsDateBO.setCount(vehicleAccessRecordStatisticsDatePO.getCount());
                }
            });
        });

//        if (type.equals(0)) {
//            for(int i=1; i<=24; i++) {
//                Date startDate = AboutDateUtil.datePlusHour(startTime, i - 1);
//                long start = startDate.getTime();
//                Date endDate = AboutDateUtil.datePlusHour(startTime, i);
//                long end = endDate.getTime();
//                long count = enterVehicleAccessRecordList.stream().filter(vehcCarRecord -> {
//                    long time = vehcCarRecord.getEnterTime().getTime();
//                    return (time >= start && time < end);
//                }).count();
//
//                VehicleAccessRecordStatisticsDateBO vehicleAccessRecordStatisticsDateBO = new VehicleAccessRecordStatisticsDateBO();
//                vehicleAccessRecordStatisticsDateBO.setDate(startDate);
//                vehicleAccessRecordStatisticsDateBO.setCount((int) count);
//                vehicleAccessRecordStatisticsBO.getEnter().add(vehicleAccessRecordStatisticsDateBO);
//            }
//        }
//        if (type.equals(1)) {
//            for(int i=1; i<=7; i++) {
//                Date startDate = AboutDateUtil.datePlusDay(startTime, i - 1);
//                long start = startDate.getTime();
//                Date endDate = AboutDateUtil.datePlusDay(startTime, i);
//                long end = endDate.getTime();
//                long count = enterVehicleAccessRecordList.stream().filter(vehcCarRecord -> {
//                    long time = vehcCarRecord.getEnterTime().getTime();
//                    return (time >= start && time < end);
//                }).count();
//
//                VehicleAccessRecordStatisticsDateBO vehicleAccessRecordStatisticsDateBO = new VehicleAccessRecordStatisticsDateBO();
//                vehicleAccessRecordStatisticsDateBO.setDate(startDate);
//                vehicleAccessRecordStatisticsDateBO.setCount((int) count);
//                vehicleAccessRecordStatisticsBO.getEnter().add(vehicleAccessRecordStatisticsDateBO);
//            }
//        }
//        if (type.equals(2)) {
//            for(int i=1; i<=30; i++) {
//                Date startDate = AboutDateUtil.datePlusDay(startTime, i - 1);
//                long start = startDate.getTime();
//                Date endDate = AboutDateUtil.datePlusDay(startTime, i);
//                long end = endDate.getTime();
//                long count = enterVehicleAccessRecordList.stream().filter(vehcCarRecord -> {
//                    long time = vehcCarRecord.getEnterTime().getTime();
//                    return (time >= start && time < end);
//                }).count();
//
//                VehicleAccessRecordStatisticsDateBO vehicleAccessRecordStatisticsDateBO = new VehicleAccessRecordStatisticsDateBO();
//                vehicleAccessRecordStatisticsDateBO.setDate(startDate);
//                vehicleAccessRecordStatisticsDateBO.setCount((int) count);
//                vehicleAccessRecordStatisticsBO.getEnter().add(vehicleAccessRecordStatisticsDateBO);
//            }
//        }
//        if (type.equals(3)) {
//            for(int i=1; i<=12; i++) {
//                Date startDate = AboutDateUtil.datePlusMonth(startTime, i - 1);
//                long start = startDate.getTime();
//                Date endDate = AboutDateUtil.datePlusMonth(startTime, i);
//                long end = endDate.getTime();
//                long count = enterVehicleAccessRecordList.stream().filter(vehcCarRecord -> {
//                    long time = vehcCarRecord.getEnterTime().getTime();
//                    return (time >= start && time < end);
//                }).count();
//
//                VehicleAccessRecordStatisticsDateBO vehicleAccessRecordStatisticsDateBO = new VehicleAccessRecordStatisticsDateBO();
//                vehicleAccessRecordStatisticsDateBO.setDate(startDate);
//                vehicleAccessRecordStatisticsDateBO.setCount((int) count);
//                vehicleAccessRecordStatisticsBO.getEnter().add(vehicleAccessRecordStatisticsDateBO);
//            }
//        }
//        if (type.equals(-1) && null!=startTime && null!=endTime) {
//            int day = 1;
//            while (true) {
//
//                if (day > 365) {
//                    throw new BusinessException("查询区间太大，超出合理范围");
//                }
//
//                Date startDate = AboutDateUtil.datePlusDay(startTime, day - 1);
//                long start = startDate.getTime();
//                Date endDate = AboutDateUtil.datePlusDay(startTime, day);
//                long end = endDate.getTime();
//
//                if (end>endTime.getTime()) {
//                    break;
//                }
//                long count = enterVehicleAccessRecordList.stream().filter(vehcCarRecord -> {
//                    long time = vehcCarRecord.getEnterTime().getTime();
//                    return (time >= start && time < end);
//                }).count();
//
//                VehicleAccessRecordStatisticsDateBO vehicleAccessRecordStatisticsDateBO = new VehicleAccessRecordStatisticsDateBO();
//                vehicleAccessRecordStatisticsDateBO.setDate(startDate);
//                vehicleAccessRecordStatisticsDateBO.setCount((int) count);
//                vehicleAccessRecordStatisticsBO.getEnter().add(vehicleAccessRecordStatisticsDateBO);
//
//                day++;
//            }
//        }

//        if (type.equals(0)) {
//            for(int i=1; i<=24; i++) {
//                Date startDate = AboutDateUtil.datePlusHour(startTime, i - 1);
//                long start = startDate.getTime();
//                Date endDate = AboutDateUtil.datePlusHour(startTime, i);
//                long end = endDate.getTime();
//                long count = existVehicleAccessRecordList.stream().filter(vehcCarRecord -> {
//                    long time = vehcCarRecord.getExistTime().getTime();
//                    return (time >= start && time < end);
//                }).count();
//
//                VehicleAccessRecordStatisticsDateBO vehicleAccessRecordStatisticsDateBO = new VehicleAccessRecordStatisticsDateBO();
//                vehicleAccessRecordStatisticsDateBO.setDate(startDate);
//                vehicleAccessRecordStatisticsDateBO.setCount((int) count);
//                vehicleAccessRecordStatisticsBO.getExist().add(vehicleAccessRecordStatisticsDateBO);
//            }
//        }
//        if (type.equals(1)) {
//            for(int i=1; i<=7; i++) {
//                Date startDate = AboutDateUtil.datePlusDay(startTime, i - 1);
//                long start = startDate.getTime();
//                Date endDate = AboutDateUtil.datePlusDay(startTime, i);
//                long end = endDate.getTime();
//                long count = existVehicleAccessRecordList.stream().filter(vehcCarRecord -> {
//                    long time = vehcCarRecord.getExistTime().getTime();
//                    return (time >= start && time < end);
//                }).count();
//
//                VehicleAccessRecordStatisticsDateBO vehicleAccessRecordStatisticsDateBO = new VehicleAccessRecordStatisticsDateBO();
//                vehicleAccessRecordStatisticsDateBO.setDate(startDate);
//                vehicleAccessRecordStatisticsDateBO.setCount((int) count);
//                vehicleAccessRecordStatisticsBO.getExist().add(vehicleAccessRecordStatisticsDateBO);
//            }
//        }
//        if (type.equals(2)) {
//            for(int i=1; i<=30; i++) {
//                Date startDate = AboutDateUtil.datePlusDay(startTime, i - 1);
//                long start = startDate.getTime();
//                Date endDate = AboutDateUtil.datePlusDay(startTime, i);
//                long end = endDate.getTime();
//                long count = existVehicleAccessRecordList.stream().filter(vehcCarRecord -> {
//                    long time = vehcCarRecord.getExistTime().getTime();
//                    return (time >= start && time < end);
//                }).count();
//
//                VehicleAccessRecordStatisticsDateBO vehicleAccessRecordStatisticsDateBO = new VehicleAccessRecordStatisticsDateBO();
//                vehicleAccessRecordStatisticsDateBO.setDate(startDate);
//                vehicleAccessRecordStatisticsDateBO.setCount((int) count);
//                vehicleAccessRecordStatisticsBO.getExist().add(vehicleAccessRecordStatisticsDateBO);
//            }
//        }
//        if (type.equals(3)) {
//            for(int i=1; i<=12; i++) {
//                Date startDate = AboutDateUtil.datePlusMonth(startTime, i - 1);
//                long start = startDate.getTime();
//                Date endDate = AboutDateUtil.datePlusMonth(startTime, i);
//                long end = endDate.getTime();
//                long count = existVehicleAccessRecordList.stream().filter(vehcCarRecord -> {
//                    long time = vehcCarRecord.getExistTime().getTime();
//                    return (time >= start && time < end);
//                }).count();
//
//                VehicleAccessRecordStatisticsDateBO vehicleAccessRecordStatisticsDateBO = new VehicleAccessRecordStatisticsDateBO();
//                vehicleAccessRecordStatisticsDateBO.setDate(startDate);
//                vehicleAccessRecordStatisticsDateBO.setCount((int) count);
//                vehicleAccessRecordStatisticsBO.getExist().add(vehicleAccessRecordStatisticsDateBO);
//            }
//        }
//        if (type.equals(-1) && null!=startTime && null!=endTime) {
//            int day = 1;
//            while (true) {
//
//                if (day > 365) {
//                    throw new BusinessException("查询区间太大，超出合理范围");
//                }
//
//                Date startDate = AboutDateUtil.datePlusDay(startTime, day - 1);
//                long start = startDate.getTime();
//                Date endDate = AboutDateUtil.datePlusDay(startTime, day);
//                long end = endDate.getTime();
//
//                if (end>endTime.getTime()) {
//                    break;
//                }
//                long count = existVehicleAccessRecordList.stream().filter(vehcCarRecord -> {
//                    long time = vehcCarRecord.getExistTime().getTime();
//                    return (time >= start && time < end);
//                }).count();
//
//                VehicleAccessRecordStatisticsDateBO vehicleAccessRecordStatisticsDateBO = new VehicleAccessRecordStatisticsDateBO();
//                vehicleAccessRecordStatisticsDateBO.setDate(startDate);
//                vehicleAccessRecordStatisticsDateBO.setCount((int) count);
//                vehicleAccessRecordStatisticsBO.getExist().add(vehicleAccessRecordStatisticsDateBO);
//
//                day++;
//            }
//        }

        vehicleAccessRecordStatisticsBO.setEnterAccessRecordNumber(enterAccessRecordNumber);
        vehicleAccessRecordStatisticsBO.setEnterLocalVehicleNumber((int)enterLocalVehicleNumber);
        vehicleAccessRecordStatisticsBO.setEnterForeignVehicleNumber(enterForeignVehicleNumber);
        vehicleAccessRecordStatisticsBO.setExistAccessRecordNumber(existAccessRecordNumber);
        vehicleAccessRecordStatisticsBO.setExistLocalVehicleNumber((int) existLocalVehicleNumber);
        vehicleAccessRecordStatisticsBO.setExistForeignVehicleNumber(existForeignVehicleNumber);
        return vehicleAccessRecordStatisticsBO;
    }

}
